<?php

namespace App\Dao\Task;

use App\Model\Exam\Exam;
use App\Model\Task\Task;
use App\Model\Task\TaskReceive;
use App\Model\Task\TaskTypes;
use Yj\Daos\BaseDao;
use Yj\Daos\Verify;

/**
 * @var TaskReceive
 */
class TaskReceiveDao extends BaseDao
{
    public function DaoWhereStatus($status)
    {
        return $this->getDaoQuery(['status' => $status], function (Verify $verify) {
            $verify('status', ['all', null], function ($query, $status) {
                $query->where('status', $status);
            });
        });
    }

    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify('id', function ($query, $id) {
                $query->where('id', $id);
            });
            $verify('task_receive_id', function ($query, $id) {
                $query->where('id', $id);
            });
            $verify('user_id', function ($query, $user_id) {
                $query->where('user_id', $user_id);
            });
            $verify('task_id', function ($query, $task_id) {
                $query->where('task_id', $task_id);
            });

        });
    }

    public function getUnFinishId($userId, $taskId)
    {
        if ($userId == 0) {
            return 0;
        }
        return $this->DaoWhere([
            'user_id' => $userId,
            'task_id' => $taskId,
        ])->where('status', '>=', 0)
            ->where('status', '<', 100)->value('id');
    }

    public function getDetail($userId, $taskId)
    {
        if ($userId == 0) {
            return false;
        }
        return $this->DaoWhere([
            'user_id' => $userId,
            'task_id' => $taskId,
        ])->orderByDesc('id')->with('TaskReceiveDetail')->first();
    }

    public function getDetailById($id)
    {

        return $this->DaoWhere([
            'id' => $id
        ])->with('TaskReceiveDetail')->first();
    }

    public function isReceive($userId, $taskId)
    {
        if ($userId == 0) {
            return false;
        }
        return $this->DaoWhere([
            'user_id' => $userId,
            'task_id' => $taskId,
        ])->where('status', '>=', 0)->where('status', '!=', 100)->exists();
    }

    public function cancel(int $taskReceiveid, int $userId)
    {
        $taskReceive = $this->DaoWhere([
            'id' => $taskReceiveid,
            'user_id' => $userId,
        ])->lockForUpdate()->firstOr(function () {
            _Exception('已取消');
        });
        if(!in_array($taskReceive->status,[0,5])){
            _Exception('禁止取消'.$taskReceive->status);
        }

        $taskReceive->delete();
        return $taskReceive;
    }

    public function isCancel(mixed $taskReceiveid)
    {
        if (!$this->DaoWhere(['id' => $taskReceiveid])->exists()) {
            return false;
        }
        return $this->DaoWhere(['id' => $taskReceiveid])->value('cancel_time') <= time();
    }

    public function getFinishedNum(mixed $task_id)
    {
        return $this->DaoWhere(compact('task_id'))->where('status', 1)->count();
    }
}